package com.example.shangjinlieren.utils;

import com.example.shangjinlieren.ResourceTable;
import ohos.agp.components.AttrHelper;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;

public class DisplayUtils {
   private float wRatio;
   private float hRatio;
   private Context context;
    public DisplayUtils(Context context,float width,float height) {
        this.context = context;
        this.wRatio = getDisplayWidthInPx()/width;
        this.hRatio = getDisplayHeightInPx()/height;
    }

    /**
     * 获取屏幕宽度
     *
     * @return 屏幕宽度
     */
    public  int getDisplayWidthInPx() {
        Display display = DisplayManager.getInstance().getDefaultDisplay(this.context).get();
        return display.getAttributes().width;
    }
    /**
     * 获取屏幕高度，不包含状态栏的高度
     * @return 屏幕高度，不包含状态栏的高度
     */
    public  int getDisplayHeightInPx() {
        Display display = DisplayManager.getInstance().getDefaultDisplay(this.context).get();
        return   display.getAttributes().height;
    }

    /**
     * vp转像素
     *
     *
     * @param vp
     * @return
     */
    public  float vp2px( float vp) {
        DisplayAttributes attributes = DisplayManager.getInstance().getDefaultDisplay(this.context).get().getAttributes();
        return  (attributes.densityPixels * vp);
    }
    public  float pxtpxW(float px){
        return  this.wRatio*px;
    }
    public  float pxtpxH(float px){
        return  this.hRatio*px;
    }
    /**
     * px 转 vp
     * @param px
     * @return
     */
    public  float rpx(int px){
        float h =0;
        if (px<=750){
            h= pxtpxW(px);
        }else if (px>750){
            h=pxtpxH(px);
        }
        return   h;
    }
}
